<?php
/**
 * Created by PhpStorm.
 * User: j28981
 * Date: 2015/8/24
 * Time: 14:46
 * 对商品分类表进行数据操作的模型类
 */
class cateModel extends dbo{
protected $table = "category";
    public function getAllCateList(){
        $sql = "select * from {$this->getTable()}";

       return $cate_list = $this->queryAll($sql);
    }
    public function getAllCateReal(){
        $cate_list = $this->getAllCateList();
        return $this->dealCateList($cate_list);
    }

    /**
     *只取出顶级分类
     */
    public function getCateLevel0(){
        $sql = "select * from {$this->getTable()} where cate_parent_id=0";
        return $this->queryAll($sql);
    }

    /**
     * @param $cate_list：查出的所有分类列表
     * @param int $p_id：默认从顶级分类查起
     * @param int $level：添加给分类的等级属性，顶级是0，依次增加
     * @return array：对所有分类进行关系重排并加上等级属性的数组列表
     */
    private function dealCateList($cate_list,$p_id=0,$level=0){
        static $cate_list_new = array();
        foreach($cate_list as $k => $cate_row){
            if($cate_row['cate_parent_id']==$p_id){
                $cate_row['level'] = $level;
                $sql = "update {$this->getTable()} set level=$level where cate_id={$cate_row['cate_id']}";
                $this->Update($sql);
                $cate_list_new[] = $cate_row;
                unset($cate_list[$k]);
                $this->dealCateList($cate_list,$cate_row['cate_id'],$level+1);
            }
        }

        return $cate_list_new;
    }

    /**
     * @param $field
     * @param $value
     * @return array_row：按照分类名称及对应的值查询出一个分类数组
     */
    public function getThisCateAndChildren($field,$value){
        $sql = "select * from {$this->getTable()} where $field = '$value'";
        $cate_row = $this->queryRow($sql);
        return $cate_row;
    }
    public function insertOneCate($cate_name,$cate_parent_id){
        $sql = "insert into {$this->getTable()} (cate_name,cate_parent_id) values ('$cate_name','$cate_parent_id')";
        return $this->Insert($sql);

    }
    public function checkCateName($cate_name,$cate_id=0){
        $sql = "select * from {$this->getTable()} where cate_name='$cate_name'";
        if($cate_id!=0){
            $sql.=" and cate_id !='$cate_id'";
        }
        return $this->queryRow($sql);
    }
    public function getCateById($cate_id){
        $sql = "select * from {$this->getTable()} where cate_id='$cate_id'";
        return $this->queryRow($sql);
    }
    public function updateCateById($cate_id,$cate_name,$cate_parent_id){
        $sql = "update {$this->getTable()} set cate_name='$cate_name',cate_parent_id='$cate_parent_id' where cate_id='$cate_id'";
        return $this->Update($sql);
    }
    public function cateSelfAndChildren($cate_id){
        $cate_list = $this->getAllCateList();

        return $this->getAllChildIds($cate_list,$cate_id);

    }

    /**
     * @param $cate_list
     * @param $cate_id
     * @return array:给定分类下的所有子类的ID数组
     */
    private function getAllChildIds($cate_list,$cate_id){
        static $cate_ids = array();    //分类Id的数组
        //var_dump($cate_list);die;
        foreach($cate_list as $k=>$v){
            if($v['cate_parent_id']==$cate_id){
                $cate_ids[]=$v['cate_id'];
                unset($cate_list[$k]);
                $this->getAllChildIds($cate_list,$v['cate_id']);
            }
        }
        $cate_ids[] = $cate_id;
        return $cate_ids;
    }

    /**
     * @param $cate_id
     * @return bool：如果有下级返回false，没有下级说明是最后一级，返回true
     */
    public function is_leaf($cate_id){
        $sql = "select * from {$this->getTable()} where cate_parent_id ='$cate_id'";
        $res = $this->queryRow($sql);

        return $res?false:true;
    }
    public function deleteCateById($cate_id){
        $sql = "delete from {$this->getTable()} where cate_id='$cate_id'";
        return $this->Delete($sql);
    }
    public function deleteCates($cate_ids){
        $cate_ids= implode(',',$cate_ids);
        $sql="delete from {$this->getTable()} where cate_id in ($cate_ids)";
        return $this->Delete($sql);
    }
    public function getCateSons($cate_id){
        $sql = "select * from {$this->getTable()} where cate_parent_id='$cate_id'";
        return $this->queryAll($sql);
    }

    /**
     *
     */
    public function cateSel(){
       // $cate_model = new cateModel();
        $cate_list =  $this->getCateLevel0();
        include_once TPLS_DIR.DS."cate_sel_tpl.php";
    }
}